package com.smartEdu.utils;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.http.Consts;
import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpRequestBase;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import com.smartEdu.commons.Tag_Http_Tips;
import com.smartEdu.commons.Tag_Tips;

public class HttpUtil {
		private HttpPost httpPost;
		private HttpGet httpGet;
		private CloseableHttpClient httpClient;
		
		public HttpUtil(){
			setHttpClient(HttpClientBuilder.create().build());
		}
		
		public CloseableHttpClient getHttpClient() {
			return httpClient;
		}

		public void setHttpClient(CloseableHttpClient httpClient) {
			this.httpClient = httpClient;
		}

		public HttpPost getHttpPost() {
			return httpPost;
		}

		public void setHttpPost(HttpPost httpPost) {
			this.httpPost = httpPost;
		}

		public HttpGet getHttpGet() {
			return httpGet;
		}

		public void setHttpGet(HttpGet httpGet) {
			this.httpGet = httpGet;
		}

		public static RequestConfig GetRequestConfig(){
			RequestConfig requestConfig = RequestConfig.custom()  
	            .setConnectionRequestTimeout(5000)
	            .setConnectTimeout(5000)  
	            .setSocketTimeout(5000).build(); 
			return requestConfig;
		}
		
		public static RequestConfig GetRequestConfig(int ConnectionRequestTimeout,int ConnectTimeout,int SocketTimeout){
			RequestConfig requestConfig = RequestConfig.custom()  
	            .setConnectionRequestTimeout(ConnectionRequestTimeout)
	            .setConnectTimeout(ConnectTimeout)  
	            .setSocketTimeout(SocketTimeout).build(); 
			return requestConfig;
		}
			
		public static void SetHeader(HttpRequestBase requestBase,String host,String loginUrl){
			requestBase.setHeader("Host", host);
			requestBase.setHeader("Referer", loginUrl);
			requestBase.setHeader("User-Agent","Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; rv:11.0) like Gecko");
			requestBase.setHeader("Content-Type", "application/x-www-form-urlencoded; charset=utf-8");
		}
		
		public static void SetHeader(HttpRequestBase requestBase,Map<String, String> headerMap){
			for (String key : headerMap.keySet()) {
				requestBase.setHeader(key, headerMap.get(key));
			}
		}
		
		public String RequestPost(List<NameValuePair> params) throws ClientProtocolException, IOException {
		    httpPost.setEntity(new UrlEncodedFormEntity(params,Consts.UTF_8));
		    CloseableHttpResponse response = this.httpClient.execute(httpPost);
		    System.out.println(response.toString());
		    HttpEntity entity = response.getEntity();
		    String result = EntityUtils.toString(entity, "utf-8");
		    System.out.println(result);
		    return result;
		}
		
		public String RequestGet() throws ClientProtocolException, IOException {  
	        httpGet.setConfig(GetRequestConfig()); 
	        CloseableHttpResponse response = this.httpClient.execute(httpGet);        
	        System.out.println("StatusCode -> " + response.getStatusLine().getStatusCode());
	        HttpEntity entity = response.getEntity();        
	        String result = EntityUtils.toString(entity);
	        System.out.println(result);
	        return result;
		}
		
		public static List<NameValuePair> SetRequestParams_default6(Tag_Http_Tips tips,Map<String, String> paramMap){
			List<NameValuePair> param=new ArrayList<NameValuePair>();
			param.add(new BasicNameValuePair("__VIEWSTATE",tips.__VIEWSTATE));
			param.add(new BasicNameValuePair("tname",tips.tname));
			param.add(new BasicNameValuePair("tbtns",tips.tbtns));
			param.add(new BasicNameValuePair("tnameXw",tips.tnameXw));
			param.add(new BasicNameValuePair("tbtnsXw",tips.tbtnsXw));
			param.add(new BasicNameValuePair("txtYhm",paramMap.get("USERNAME")));
			param.add(new BasicNameValuePair("txtXm",paramMap.get("PASSWORD")));
			param.add(new BasicNameValuePair("txtMm",paramMap.get("PASSWORD")));
			if (paramMap.get(tips.userType).toString().equals(Tag_Tips.TEACHER)) {
				param.add(new BasicNameValuePair("rblJs",tips.rblJs_teacher));
			}
			else if (paramMap.get(tips.userType).toString().equals(Tag_Tips.STUDENT)) {
				param.add(new BasicNameValuePair("rblJs",tips.rblJs_student));
			}
			param.add(new BasicNameValuePair("btnDl",tips.btnDl));
			return param;
		}
		
		public void releaseConnection(){
			if (httpGet!=null) {
				httpGet.releaseConnection();
			}
			if (httpPost!=null) {
				httpPost.releaseConnection();
			}
		}
		
		public void closeHttpClient(){
			try {
				this.httpClient.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
			finally {
				this.httpClient=null;
			}
		}
		
}
